using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComputerMediator : BaseLazySingleton<ComputerMediator>
{

    private Termina curTermina;
    private Peripheral curPeripheral;
    private LinkStrategy curLinkStrategy;

    private List<Termina> terminas = new List<Termina>();
    private List<Peripheral> peripherals = new List<Peripheral>();
    private List<LinkStrategy> linkStrategys = new List<LinkStrategy>();
    
    public ComputerMediator()
    {
        InitTerminas();
        InitPeripherals();
        InitLinkStrategies();
    }
    private void InitTerminas()
    {
        terminas.Add(new Computer());
        terminas.Add(new Phone());
        terminas.Add(new HuaweiPhone());
    }

    private void InitPeripherals()
    {
        peripherals.Add(new Mouse());
        peripherals.Add(new Keyboard());
    }

    private void InitLinkStrategies()
    {
        linkStrategys.Add(new WiredConnection());
        linkStrategys.Add(new WirelessConnection());
    }


    public List<Termina> GetTerminas()
    {
        return terminas;
    }
    public List<Peripheral> GetPeripherals()
    {
        return peripherals; 
    }
    public List<LinkStrategy> GetLinkStrategys()
    {
        return linkStrategys;
    }
    
    
    /// <summary>
    /// 链接外设与终端
    /// </summary>
    /// <param name="peripheralIndex"></param>
    /// <param name="linkStrategyIndex"></param>
    /// <param name="terminaIndex"></param>
    public void ConnectPeripheralAndTermina(int peripheralIndex, int linkStrategyIndex, int terminaIndex)
    {
        if (peripheralIndex >= peripherals.Count 
            || linkStrategyIndex >= linkStrategys.Count
            || terminaIndex >= terminas.Count)
        {
            Debug.Log($" [ComputerMediator] ConnectPeripheralAndTermina indexError");
            return;
        }

        if (curPeripheral != null)
        {
            curPeripheral.ReSetPeripheral();
        }
        
        curPeripheral = peripherals[peripheralIndex];
        curLinkStrategy = linkStrategys[linkStrategyIndex];
        curTermina = terminas[terminaIndex];
        curPeripheral.Init(curTermina, curLinkStrategy);
    }

    /// <summary>
    /// 发送消息
    /// </summary>
    public string SendMessage()
    {
        if (curPeripheral != null)
        {
            return curPeripheral.SendMessage();
        }
        else
        {
            Debug.Log($" [ComputerMediator] SendMessage error curPeripheral is null");
            return null;
        }
    }

    /// <summary>
    /// 重置所有设备
    /// </summary>
    public void ReSetAll()
    {
        if (curPeripheral != null)
        {
            curPeripheral.ReSetPeripheral();
        }

        curPeripheral = null;
        curLinkStrategy = null;
        curTermina = null;
    }
}


public enum eItemType
{
    Peripheral = 0,
    Termina = 1,
    LinkStrategy = 2,
}

